package cn.edu.ustc.xrovers.serviceImpl;

import javax.servlet.http.HttpSession;

import cn.edu.ustc.xrovers.dao.CompanyInfoDao;
import cn.edu.ustc.xrovers.model.BalanceSheet;
import cn.edu.ustc.xrovers.model.CashFlowStatement;
import cn.edu.ustc.xrovers.model.Company;
import cn.edu.ustc.xrovers.model.IncomeStatement;
import cn.edu.ustc.xrovers.model.Loan;
import cn.edu.ustc.xrovers.model.Manager;
import cn.edu.ustc.xrovers.model.NonFinancialInformation;
import cn.edu.ustc.xrovers.service.HandleCompanyInfoService;

/**
 * 处理企业信息的Service实现类
 * @author paddy
 *
 */
public class HandleCompanyInfoServiceImpl implements HandleCompanyInfoService {
	
	private CompanyInfoDao cid;
	
	/**
	 * 保存企业基本信息，并将其信息放入session中
	 * @param session HttpSession对象
	 * @param company Company持久化对象
	 * @return 保存成功返回true，否则返回false
	 */
	public boolean saveCoBaseInfo(HttpSession session, Company company, final String role) {
		boolean flag=cid.saveBaseInfo(company, role);
		if(!flag)
			return false;
		else if(session==null)
			return true;
		else{
			session.setAttribute("company",company);
			return true;
		}
	}
	
	/**
	 * 保存担保企业的基本信息
	 * @param session HttpSession
	 * @param company Company持久化对象
	 * @return 保存成功返回true，否则返回false
	 */
	public boolean saveGuaBaseInfo(HttpSession session,Company guaCompany,final String role) {
		Company company=(Company)session.getAttribute("company");
		guaCompany.getCompanys().add(company);
		session.setAttribute("guaCompany", guaCompany);
		boolean flag=saveCoBaseInfo(null,guaCompany,role);
		if(flag){
			company.setGuaCompany(guaCompany);
			cid.updateInfo(company);
		}
		return flag;
	}
	
	/**
	 * 保存企业财务信息
	 * @param bs BalanceSheet资产负债持久化对象
	 * @param cfs CashFlowStatement现金流量持久化对象
	 * @param is IncomeStatement利润持久化对象
	 * @param company Company持久化对象
	 * @return 保存成功返回true，否则返回false
	 */
	public boolean saveCoFinInfo(HttpSession session, BalanceSheet bs, CashFlowStatement cfs, IncomeStatement is) {
		Company company=(Company)session.getAttribute("company");
		return cid.saveFinInfo(bs, cfs, is, company);
	}
	
	/**
	 * 保存担保企业财务信息
	 * @param bs BalanceSheet资产负债持久化对象
	 * @param cfs CashFlowStatement现金流量持久化对象
	 * @param is IncomeStatement利润持久化对象
	 * @param company Company持久化对象
	 * @return 保存成功返回true，否则返回false
	 */
	public boolean saveGuaFinInfo(HttpSession session,BalanceSheet bs, CashFlowStatement cfs, IncomeStatement is) {
		Company guaCompany=(Company)session.getAttribute("guaCompany");
		return cid.saveFinInfo(bs, cfs, is, guaCompany);
	}
	
	/**
	 * 保存企业非财务信息
	 * @param nfi NonFinancialInformation持久化类
	 * @param company Company持久化类
	 * @return 保存成功返回true，否则返回false
	 */
	public boolean saveCoNonFinInfo(HttpSession session, NonFinancialInformation nfi){
		Company company=(Company)session.getAttribute("company");
		boolean flag=cid.saveNonFinInfo(nfi, company);
		return flag;
	}

	/**
	 * 保存担保企业非财务信息
	 * @param nfi NonFinancialInformation持久化类
	 * @param company Company持久化类
	 * @return 保存成功返回true，否则返回false
	 */
	public boolean saveGuaNonFinInfo(HttpSession session, NonFinancialInformation nfi) {
		Company guaCompany=(Company)session.getAttribute("guaCompany");
		System.out.println(guaCompany);
		boolean flag=cid.saveNonFinInfo(nfi, guaCompany);
		return flag;
	}
	
	/**
	 * 保存贷款基本信息
	 * 金额，担保方式，日期
	 * @param session HttpSession
	 * @param loan Loan实体类对象，贷款基本信息
	 * @return 保存成功返回true，否则返回false
	 */
	public boolean saveLoanInfo(HttpSession session, Loan loan, String guaWay) {
		Company company=(Company)session.getAttribute("company");
		Manager manager=(Manager)session.getAttribute("userInfo");
		boolean flag=cid.saveLoanInfo(loan, guaWay, company, manager);
		if(!flag)
			return false;
		else{
			session.removeAttribute("company");
			session.removeAttribute("guaCompany");
			return true;
		}
	}
	
	/**
	 * 根据报表名称查询相应报表
	 * @param session HttpSession对象
	 * @param reportname 报表名称
	 */
	public Object findReport(String reportname, String loanId) {
		 return cid.findReportInfoByLoanId(reportname, loanId);
	}
	
	/**
	 * 根据贷款编号查找贷款信息
	 * @param loanId 贷款编号
	 * @return Loan实体类对象
	 */
	public Object findLoanInfo(String loanId) {
		return cid.findLoanInfoByLoanId(loanId);
	}
	
	/**
	 * 更新报表信息
	 * @param entity 报表信息实体类
	 */
	public void updateInfo(Object entity) {
		cid.updateInfo(entity);
	}
	
	/*getset方法*/
	public void setCid(CompanyInfoDao cid) {
		this.cid = cid;
	}
	public CompanyInfoDao getCid() {
		return cid;
	}



}
